package com.breezelin.freshmarket.entities.data;

/**
 * Created by Breeze Lin
 * 2016/5/16 09:24
 * breezesummerlin@163.com
 */

import android.os.Parcel;
import android.os.Parcelable;

import com.breezelin.freshmarket.entities.JsonStruct;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品分类目录条目类
 */
public class Category implements JsonStruct, Parcelable {

	/*
	"CategoryId": "ad7f227d-73c0-44a2-9edd-924006deb134",
        "CategoryCode": "01_channelhome",
        "CategoryName": "进口水果",
        "PictureUrl": "http://img02.yiguo.com/e/web/140418/00872/cats/01_channelhome.png",
        "Icon": "http://img02.yiguo.com/e/web/130101/app/cats/01_channelhome.png",
        "SmallIcon": "http://img02.yiguo.com/e/web/130101/app/cats/small01_channelhome.png",
        "SmallSelectedIcon": "http://img02.yiguo.com/e/web/130101/app/cats/smallselected01_channelhome.png",
        "SmallUnSelectedIcon": "http://img02.yiguo.com/e/web/130101/app/cats/smallunselected01_channelhome.png",
        "Description": "奇异果/车厘子/牛油果/柑桔橙柚",
        "Childs": []
      }
	 */

	/**
	 * 目录id
	 */
	private String categoryId;
	/**
	 * 目录代码
	 */
	private String categoryCode;
	/**
	 * 目录名
	 */
	private String categoryName;
	/**
	 * 图片链接（不需要）
	 */
	private String pictureUrl;
	/**
	 * 类别图标
	 */
	private String icon;
	/**
	 * 小图标
	 */
	private String smallIcon;
	/**
	 * 选择后的图标
	 */
	private String smallSelectedIcon;
	/**
	 * 未选中的图标
	 */
	private String smallUnselectedIcon;
	/**
	 * 描述语言
	 */
	private String description;
	/**
	 * 示例产品
	 */
	private List<ProductShortCut> childs;

	public Category() {
	}

	public Category(JSONObject jsonObject) {
		fromJson(jsonObject);
	}

	public String getCategoryId() {
		return categoryId;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public String getCategoryCode() {
		return categoryCode;
	}

	public String getPictureUrl() {
		return pictureUrl;
	}

	public String getIcon() {
		return icon;
	}

	public String getSmallIcon() {
		return smallIcon;
	}

	public String getSmallSelectedIcon() {
		return smallSelectedIcon;
	}

	public String getSmallUnselectedIcon() {
		return smallUnselectedIcon;
	}

	public String getDescription() {
		return description;
	}

	public List<ProductShortCut> getChilds() {
		return childs;
	}

	@Override
	public void fromJson(JSONObject jsonObject) {
		categoryId = jsonObject.optString("CategoryId");
		categoryCode = jsonObject.optString("CategoryCode");
		categoryName = jsonObject.optString("CategoryName");
		pictureUrl = jsonObject.optString("PictureUrl");
		icon = jsonObject.optString("Icon");
		smallIcon = jsonObject.optString("SmallIcon");
		smallSelectedIcon = jsonObject.optString("SmallSelectedIcon");
		smallUnselectedIcon = jsonObject.optString("SmallUnselectedIcon");
		description = jsonObject.optString("Description");
		childs = new ArrayList<>();
		JSONArray childsJson = jsonObject.optJSONArray("Childs");
		if (childsJson != null) {
			for (int i = 0; i < childsJson.length(); i++) {
				childs.add(new ProductShortCut(childsJson.optJSONObject(i)));
			}
		}
	}


	@Override
	public JSONObject toJson() {
		return new JSONObject();
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(this.categoryId);
		dest.writeString(this.categoryCode);
		dest.writeString(this.categoryName);
		dest.writeString(this.pictureUrl);
		dest.writeString(this.icon);
		dest.writeString(this.smallIcon);
		dest.writeString(this.smallSelectedIcon);
		dest.writeString(this.smallUnselectedIcon);
		dest.writeString(this.description);
		dest.writeList(this.childs);
	}

	protected Category(Parcel in) {
		this.categoryId = in.readString();
		this.categoryCode = in.readString();
		this.categoryName = in.readString();
		this.pictureUrl = in.readString();
		this.icon = in.readString();
		this.smallIcon = in.readString();
		this.smallSelectedIcon = in.readString();
		this.smallUnselectedIcon = in.readString();
		this.description = in.readString();
		this.childs = new ArrayList<ProductShortCut>();
		in.readList(this.childs, ProductShortCut.class.getClassLoader());
	}

	public static final Parcelable.Creator<Category> CREATOR = new Parcelable.Creator<Category>() {
		@Override
		public Category createFromParcel(Parcel source) {
			return new Category(source);
		}

		@Override
		public Category[] newArray(int size) {
			return new Category[size];
		}
	};
}
